﻿@using CoralNode.Core.Entity.Domains.Site
@using CoralNode.Core.Entity.Domains.User
@using CoralNode.Core.Entity.Enums
@using CoralNode.MvcClient.Helper
@using Coralcode.Base.Entity
@using Coralcode.Utils.Basic
@using CoralNode.MvcClient.Contexts
@model CoralNode.Core.Entity.Domains.User.RoleDomain

@{
    ViewBag.Title = "Edit";
    IList<PermissionDomain> systemPermissionList = ViewBag.SystemPermissionList;
    IList<PermissionDomain> webPermissionList = ViewBag.WebPermissionList;
    IList<RolePermissionDomain> rolePermissionList = ViewBag.RolePermissionList;
    IList<SiteDomain> allWorkBench = ViewBag.AllWorkbenchList;

}
@Html.IncludePage("CoralAdmin/user", "_Left")
<div id="pbody">
    <div class="extra">
        <div class="pbody_top">
            <span class="ico ico_setting_system"></span><span class="t t_c">用户管理&nbsp;&nbsp;/&nbsp;&nbsp;<b
                class="t_small f_no">角色管理</b></span>
        </div>
        <div id="main" class="userList_cnt">
            <div class="local">
                <span class="pl_1">设置角色权限</span><span class="info pl_4">
                    @Model.Name，共有<b class="num">@ViewBag.RolePermissionList.Length</b>
                    个权限</span>
            </div>
            <div class="jurisdiction_cnt">
                <form action="/m/user/SettingRole/@Model.Id" method="POST">
                    <div class="cnt">
                        <div class="list_jurisdictionlist">
                            <div class="clearfix list_cnt">
                                <ul>
                                    @foreach (var site in allWorkBench) {
                                        <li class="more js-check" itemid="@site.Id" >
                                            @if (rolePermissionList.Any(n => n.SiteId == site.Id)) {
                                                <label class="checkbox_own selected">
                                                    <input type="checkbox" checked="checked" /></label>@site.Name
                                            } else {
                                                <label class="checkbox_own">
                                                    <input type="checkbox" /></label>@site.Name
                                            }
                                            <ul class="d_n">
                                                @if (site.Status == SiteEnums.SiteStatus.Management) {
                                                    foreach (var item in systemPermissionList.Where(n => n.SiteTheme == site.ThemeCode))
                                                    {
                                                        if (ManageHtmlHelper.Contains(rolePermissionList, item.Id, site.Id)) {
                                                    <li class="more">
                                                        <label class="checkbox_own selected">
                                                            <input type="checkbox" checked="checked" value="@item.Id"/></label>@item.Title
                                                        @if (item.HasChildren == DomainEnums.Binary.True) {
                                                            <ul class="d_n">
                                                                @foreach (var c in item.Children) {
                                                                    <li>
                                                                        @if (ManageHtmlHelper.Contains(ViewBag.RolePermissionList, c.Id, site.Id)) {
                                                                            <label class="checkbox_own selected">
                                                                                <input type="checkbox" checked="checked" value="@c.Id"/></label>
                                                                            @c.Title
                                                                    
                                                                        } else {
                                                                            <label class="checkbox_own">
                                                                                <input type="checkbox" value="@c.Id"/></label>
                                                                            @c.Title
                                                                        }

                                                                    </li>
                                                                }
                                                            </ul>
                                                        }
                                                    </li>
                                                        } else {
                                                    <li>
                                                        <label class="checkbox_own">
                                                            <input type="checkbox" value="@item.Id"/></label>@item.Title
                                                        @if (item.HasChildren == DomainEnums.Binary.True) {
                                                            <ul class="d_n">
                                                                @foreach (var c in item.Children) {
                                                                    <li>
                                                                        @if (ManageHtmlHelper.Contains(ViewBag.RolePermissionList, c.Id, site.Id)) {
                                                                            <label class="checkbox_own selected">
                                                                                <input type="checkbox" checked="checked" value="@c.Id"/></label>
                                                                            @c.Title
                                                                    
                                                                        } else {
                                                                            <label class="checkbox_own">
                                                                                <input type="checkbox" value="@c.Id"/></label>
                                                                            @c.Title
                                                                        }

                                                                    </li>
                                                                }
                                                            </ul>
                                                        }
                                                    </li>
                                                        }
                                                    }
                                                } else {
                                                    foreach (var item in webPermissionList) {
                                                        if (ManageHtmlHelper.Contains(rolePermissionList, item.Id, site.Id)) {
                                                    <li>
                                                        <label class="checkbox_own selected">
                                                            <input type="checkbox" checked="checked" value="@item.Id"/></label>@item.Title
                                                        @if (item.HasChildren == DomainEnums.Binary.True) {
                                                            <ul class="d_n">
                                                                @foreach (var c in item.Children) {
                                                                    <li>
                                                                        @if (ManageHtmlHelper.Contains(rolePermissionList, c.Id, site.Id)) {
                                                                            <label class="checkbox_own selected">
                                                                                <input type="checkbox" checked="checked" value="@c.Id"/></label>
                                                                            @c.Title
                                                                    
                                                                        } else {
                                                                            <label class="checkbox_own">
                                                                                <input type="checkbox" value="@c.Id"/></label>
                                                                            @c.Title
                                                                        }
                                                                    </li>
                                                                }
                                                            </ul>
                                                        }</li>
                                                        } else {
                                                    <li>
                                                        <label class="checkbox_own">
                                                            <input type="checkbox" value="@item.Id"/></label>@item.Title
                                                        @if (item.HasChildren == DomainEnums.Binary.True) {
                                                            <ul class="d_n">
                                                                @foreach (var c in item.Children) {
                                                                    <li>
                                                                        @if (ManageHtmlHelper.Contains(ViewBag.RolePermissionList, c.Id, site.Id)) {
                                                                            <label class="checkbox_own selected">
                                                                                <input type="checkbox" checked="checked" value="@c.Id"/></label>
                                                                            @c.Title
                                                                    
                                                                        } else {
                                                                            <label class="checkbox_own">
                                                                                <input type="checkbox" value="@c.Id"/></label>
                                                                            @c.Title
                                                                        }

                                                                    </li>
                                                                }
                                                            </ul>
                                                        }</li>
                                                        }
                                                    }
                                                }
                                            </ul>
                                            <input type="hidden" name="RolePermissionModels" value="" />
                                        </li>
                                    }
                                </ul>
                            </div>
                        </div>
                    </div>
                    <div class="global_fun">
                        <input type="submit" class="btn_2 btn invert" value="提交" />
                        <a href="/m/user/ListRole">返回</a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    $(document).ready(function () {
        $('form').submit(function () {
            $('.js-check').each(function () {
                var $this = $(this);
                var roles = new Array();
                $($this.find('ul label.checkbox_own').each(function (i, n) {
                    if ($(n).hasClass("selected")) {
                        roles.push($(n).find('input').val());
                    }
                }));
                var result = '{"Permissions":"' + roles.join(',') + '","SiteId":"'
+ $this.attr("itemId") + '"}';
                $this.children('input[name="RolePermissionModels"]').val(result);
            });
        });
    });
</script>
